<template>
  <div class="article">
    <el-card>
      <template #header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="'/home'">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <el-form label-width="100px">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <HmSelect :form="form"></HmSelect>
        </el-form-item>
        <!-- 日期 -->
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.picker"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!--  -->
    <!--  -->
    <!--  -->
    <!--  -->
    <!-- 通过点击按钮获取请求数据后进行渲染 -->
    <el-card class="box-card">
      <template #header> 根据筛选条件共获得{{ total }}条结果 </template>
      <el-table :data="list" stripe border>
        <el-table-column label="封面">
          <template v-slot="scope">
            <img :src="scope.row.cover.images[0]" alt="" class="cover" />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template v-slot="{ row }">
            {{ row.status === 2 ? '审核通过' : '审核不通过' }}
          </template>
        </el-table-column>
        <el-table-column label="发布时间">
          <template v-slot="{ row }">
            {{ row.pubdate }}
          </template>
        </el-table-column>
        <el-table-column prop="read_count" label="阅读数"> </el-table-column>
        <el-table-column prop="comment_count" label="点击量"> </el-table-column>
        <el-table-column prop="like_count" label="点赞数"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="scope">
            <h3>{{ scope.$index }}</h3>
            <el-button @click="edit(scope.row)">编辑</el-button>
            <el-button @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column> </el-table
      >\
      <!-- 分页组件
      el-pagination
          total:总条数
          page-size:页容量
          page-sizes:页容量选项
          current-page:默认页码
          layout="通过参数配制需要的功能 total,sizes,prev,pager,next,jumper"
          @current-change:页码修改后触发function(page){page:当前页码}
          @size-change="页容量修改后触发function(size){size:当前页容量}"
    -->

      <el-pagination
        :total="total"
        :page-size="form.per_page"
        :page-sizes="[1, 10, 20, 30, 40]"
        :current-page="form.page"
        layout="total,sizes,prev,pager,next,jumper"
        @current-change="pageChange"
        @size-change="sizeChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { channels, mpArticles, mpArticlesDelete } from '@/api/index.js'
import { Message } from 'element-ui'

export default {
  data () {
    return {
      form: {
        picker: [],
        status: '', // 否文章状态，0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部
        channel_id: '', // 是不传为全部
        begin_pubdate: '', // 起始时间
        end_pubdate: '', // 否截止时间
        page: 1, // 否页码 默认为1页
        per_page: 10 // 否每页数量 不传为默认10
      },
      channelsList: [],
      list: [],
      total: 0
    }
  },
  watch: {
    'form.picker': {
      handler (newVal) {
        // console.log(newVal)
        if (newVal.length === 2) {
          this.form.begin_pubdate = newVal[0]
          this.form.end_pubdate = newVal[1]
        }
      },
      deep: true
    }
  },
  async created () {
    const res = await channels()
    // console.log(res)
    this.channelsList = res.data.channels
    this.getDate()
  },
  methods: {
    pickerChange (value) {
      if (value.length === 2) {
        this.form.begin_pubdate = value[0]
        this.form.end_pubdate = value[1]
      }
    },
    async getDate () {
      const res = await mpArticles(this.form)
      this.list = res.data.results
      this.total = res.data.total_count
    },
    submit () {
      this.getDate()
      this.form.page = 1
    },
    // 页码修改
    pageChange (page) {
      debugger
      this.form.page = page
      this.getDate()
    },
    // 页容量改变
    sizeChange (size) {
      this.form.per_page = size
      this.getDate()
      this.form.page = 1
    },
    del (row) {
      this.$confirm('确认删除该条数据吗?', '温馨提示', {
        type: 'warning',
        confirmButtonText: '确认删除',
        cancelButtonText: '点错了'
      })
        .then(async () => {
          await mpArticlesDelete(row.id)
          Message.success('删除成功')
          this.submit()
        })
        .catch(() => {
          Message.success('取消删除')
        })
    },
    edit (row) {
      this.$router.push('/publish?id=' + row.id)
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  text-align: left;
}
.cover {
  width: 50px;
}
.el-card__header {
  height: 20px;
  text-align: left;
}
</style>
